﻿using MWS.Processing.Interfaces;
using MWS.Processing.Processors;
using ThrottledProcessing.SampleApplication.Interfaces;
using ThrottledProcessing.SampleApplication.Messages;
using ThrottledProcessing.SampleApplication.Services;

namespace ThrottledProcessing.SampleApplication.Processors
{
    public class ProcessEventProcessor : DequeueProcessor<ProcessEventRequest, ProcessEventResponse>
    {
        private readonly ILoggingService loggingService;

        public ProcessEventProcessor(ILoggingService loggingService, IQueue<ProcessEventRequest> queue)
            : this(loggingService, queue, new AlertService(loggingService))
        {
        }

        public ProcessEventProcessor(ILoggingService loggingService, IQueue<ProcessEventRequest> queue,
                                     IAlertService alertService)
            : base(alertService.ProcessEvent, queue)
        {
            this.loggingService = loggingService;

            Stopping += OnStoppingLocal;
            Stopped += OnStopped;
        }

        public override void OnProcessComplete(object sender, ResponseEventArgs args)
        {
            loggingService.Debug(string.Format("Finished processing event for userId: {0}, eventId: {1}",
                                               args.Request.UserId, args.Response.EventId));
        }

        private void OnStoppingLocal(object sender, StatusChangedEventArgs args)
        {
            loggingService.Debug("============ STOPPING ALERT SERVICE! ===========");
        }

        private void OnStopped(object sender, StatusChangedEventArgs args)
        {
            loggingService.Debug("==== STOPPED ALERT SERVICE ===");
        }
    }
}